package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

public class DBConnection {
	
	private static BasicDataSource bs;
	
	public static Connection getConnection(){
		Connection conn=null;
		if(bs==null){
			bs=new BasicDataSource();
			bs.setDriverClassName(Constant.DriverName);
			bs.setUrl(Constant.Url);
			bs.setUsername(Constant.UserName);
			bs.setPassword(Constant.UserPass);
			bs.setMaxActive(80);
			bs.setMaxWait(5000);
		}
		try {
			conn=bs.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void closeSource(Connection conn,PreparedStatement ps,ResultSet rs){
		try {
			if(null!=rs){
				rs.close();
				rs=null;
			}
			if(null!=ps){
				ps.close();
				ps=null;
			}
			if(null!=conn){
				conn.close();
				conn=null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
